package cn.tedu.csmall.business.controller;


import cn.tedu.csmall.business.service.IBusinessService;
import cn.tedu.csmall.commons.exception.CoolSharkServiceException;
import cn.tedu.csmall.commons.restful.JsonResult;
import cn.tedu.csmall.commons.restful.ResponseCode;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/base/business")
@Api(tags = "购买业务开始模块")
public class BusinessController {

    @Autowired
    IBusinessService businessService;

    @PostMapping("/buy")
    @ApiOperation("发起购买")
    @SentinelResource(value = "购买商品的方法",blockHandler = "blockError",
            fallback = "fallbackError"
    )
    public JsonResult<Void> buy(){
        if(Math.random()<0.5){
            throw new CoolSharkServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"服务异常！");
        }
        businessService.buy();
        return JsonResult.ok("购买成功！");
    }
    public JsonResult blockError(){
        return JsonResult.failed(ResponseCode.BAD_REQUEST,"服务器忙");
    }
    public JsonResult fallbackError(){
        return JsonResult.failed(ResponseCode.INTERNAL_SERVER_ERROR,"服务降级");
    }


}
